package com.xhh.enjoyapp.activity;

import android.app.AppComponentFactory;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.xhh.enjoyapp.Bean.User;
import com.xhh.enjoyapp.MainActivity;
import com.xhh.enjoyapp.R;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * @description
 * @author: weiXhh
 * @create: 2020-05-13 14:42
 **/
public class Login extends AppCompatActivity {

    private EditText username, password;
    private Button btnLogin,btnToRegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        username = findViewById(R.id.et_login_username);
        password = findViewById(R.id.et_login_pwd);
        btnLogin = findViewById(R.id.btn_login);
        btnToRegister = findViewById(R.id.btn_to_register);

        btnLogin.setOnClickListener(click->{
            User user = new User();
            user.setUsername(username.getText().toString().trim());
            user.setPassword(password.getText().toString().trim());

            // Bmob用户登录
            user.login(new SaveListener<User>() {
                @Override
                public void done(User user, BmobException e) {
                    if(e == null){
                        // 登录成功
                        Toast.makeText(Login.this, "登录成功", Toast.LENGTH_LONG).show();
                        startActivity(new Intent(Login.this, MainActivity.class));
                        finish();
                    } else {
                        Log.e("LoginActivity", "失败" + e);
                        Toast.makeText(Login.this, "登录失败", Toast.LENGTH_LONG).show();
                    }
                }
            });
        });

        btnToRegister.setOnClickListener(click->{
            startActivity(new Intent(Login.this, Register.class));
        });

    }
}
